﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Dynamic;
using M;
using Mysoft.MyCM;
using Mysoft.MyCM.Scm;
using Mysoft.MyCM.Business;
using Mysoft.MyCM.Business.Scm;

namespace Mysoft.MyCM.Web.Controllers
{
	public class DashboardController : MFunViewBaseController
    {
        // GET: Dashboard
        public ActionResult Index()
        {
			CustomBLL cstBLL = new CustomBLL();
			ProjectBLL projBLL = new ProjectBLL();
			// 获取客户详细信息
			Custom cst = cstBLL.GetCustom(this.RequestParam.SelectedCustomerCode);
			// 获取客户项目信息
			List<Project> projects = projBLL.GetCustomProjects(cst.CustomGUID);
			// 调整客户的分支顺序
			projects.ForEach(proj =>
			{
				proj.Branches = proj.Branches.OrderBy(p => p.BranchNum).ToList();
			});

			dynamic model = new ExpandoObject();
			model.Customer = cst;
			model.Projects = projects;

			// 设置标题
			ViewBag.Title = string.Format("{0} - 客户仪表盘", cst.CustomName);

			// TODO: 调整产品类型。。。枚举么？

			return View(model);
        }

		/// <summary>
		/// 获取产品类型的标记字段
		/// </summary>
		/// <param name="productVersion"></param>
		/// <returns></returns>
		public static string GetProductVersionTag(string productVersion)
		{
			if (string.IsNullOrEmpty(productVersion)) return "etc";
			if (productVersion.Length < 2) return "etc";
			if (productVersion.Substring(0, 2).Equals("wf", StringComparison.CurrentCultureIgnoreCase)) return "wf";
			if (productVersion.Substring(0, 3).Equals("esb", StringComparison.CurrentCultureIgnoreCase)) return "esb";
			if (productVersion.Substring(0, 3).Equals("erp", StringComparison.CurrentCultureIgnoreCase)) return "erp";
			return "etc";
		}
    }
}